package hongcaosp.app.android.search;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.LoadMoreHandler;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.bean.UserInfo;
import hongcaosp.app.android.user.UserDetailActivity;
import hongcaosp.app.android.video.player.VideoPresenter;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class SearchUserFragment extends BaseFragment implements SearchEventListener,
        OnRecycleItemClickListener,IListView<UserInfo>,SearchUserHolder.UserFollowCall {
    private RecyclerView recyclerView;
    private BaseAdapter<UserInfo> adapter;

    private LinearLayoutManager linearLayoutManager;

    private SearchPresenter searchPresenter;

    private LoadMoreHandler loadMoreHandler;

    private View emptyToast;
    private VideoPresenter videoPresenter;

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayout.VERTICAL, false));
        recyclerView.setAdapter( adapter = new BaseAdapter<UserInfo>() {
            @NonNull
            @Override
            public SearchUserHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new SearchUserHolder(getContext(),parent,SearchUserFragment.this,
                        SearchUserFragment.this);
            }
        });

        loadMoreHandler = new LoadMoreHandler(linearLayoutManager) {
            @Override
            public void loadMore() {
                searchPresenter.loadMore();
            }
        };
        recyclerView.addOnScrollListener(loadMoreHandler);
        emptyToast = findViewById(R.id.empty_toast);
        videoPresenter = new VideoPresenter();
        searchPresenter = new SearchPresenter(null,this);
    }

    private void freshEmptyState() {
        if (adapter.getItemCount() == 0) {
            emptyToast.setVisibility(View.VISIBLE);
        } else {
            emptyToast.setVisibility(View.GONE);
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_search_user;
    }

    @Override
    public void onStartSearch(String text) {
        searchPresenter.fresh(text);
    }

    @Override
    public void freshList(List<UserInfo> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        adapter.setData(data);
        adapter.notifyDataSetChanged();
        freshEmptyState();
    }

    @Override
    public void addList(List<UserInfo> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        if (data != null) {
            adapter.addData(data);
            adapter.notifyDataSetChanged();
        }
        freshEmptyState();
    }

    @Override
    public void onItemClick(int position) {
        UserInfo userInfo = adapter.getItem(position);
        if (userInfo != null) {
            UserDetailActivity.toDetail(getContext(), userInfo.getId());
        }
    }

    @Override
    public void onItemLongClick(int position) {

    }

    @Override
    public void onUserFollow(final SearchUserHolder searchUserHolder) {
        UserInfo userInfo = searchUserHolder.getData();
        if (UserToken.getDefault().isLogged(getContext())) {
            if (videoPresenter != null) {
                videoPresenter.followUser(userInfo, new PresenterListener() {
                    @Override
                    public void onSuccess() {
                        searchUserHolder.freshFollow();
                    }

                    @Override
                    public void onFail(String message) {
                        ToastManager.getInstance().showToast(message);
                    }
                });
            }
        }
    }

    @Override
    public void onUserUnfollow(final SearchUserHolder searchUserHolder) {
        UserInfo userInfo = searchUserHolder.getData();
        if (UserToken.getDefault().isLogged(getContext())) {
            if (videoPresenter != null) {
                videoPresenter.cancelFollowUser(userInfo, new PresenterListener() {
                    @Override
                    public void onSuccess() {
                        searchUserHolder.freshFollow();
                    }

                    @Override
                    public void onFail(String message) {
                        ToastManager.getInstance().showToast(message);
                    }
                });
            }
        }
    }
}
